import type { AppRouteModule } from '@/router/types';
import { getParentLayout, LAYOUT } from '@/router/constant';

function getChildren() {
  const modules = import.meta.glob('@/views/plugins/*/route.ts', { eager: true });
  const routeList: AppRouteModule[] = [];
  for (const modulePath in modules) {
    if (Object.prototype.hasOwnProperty.call(modules, modulePath)) {
      try {
        const module = modules[modulePath];
        routeList.push(module.route);
      } catch (error) {
        console.error(`Failed to load module ${modulePath}: ${error.message}`);
      }
    }
  }
  return routeList;
}

const route: AppRouteModule = {
  path: '/plugin',
  name: 'plugin',
  component: LAYOUT,
  redirect: '/plugin',
  meta: {
    orderNo: 11000,
    icon: 'ant-design:appstore-add-outlined',
    title: '扩展中心',
  },
  children: [
    {
      path: 'index',
      name: 'pluginIndex',
      component: () => import('@/views/wex/plugin/index.vue'),
      meta: {
        title: '扩展列表',
      },
    },
    {
      path: 'installed',
      name: 'pluginInstalled',
      component: () => import('@/views/wex/plugin/installed.vue'),
      meta: {
        title: '安装管理',
      },
    },
    {
      path: 'installed-plugins',
      name: 'installedPlugins',
      component: getParentLayout('installedPlugins'),
      redirect: '/plugin/installed-plugins',
      meta: {
        title: '已安装扩展',
      },
      children: getChildren(),
    },
  ],
};

export default route;